package com.czxy.service;

import com.czxy.dao.OrderMapper;
import com.czxy.domain.Order;
import com.czxy.domain.OrderItem;
import com.czxy.utils.MyBatisUtils;
import org.junit.Test;

import java.util.Date;

public class OrderService {



    public void addOrder(Order order){

        // 把 order 的基本信息添加到 order表中
        OrderMapper mapper = MyBatisUtils.getMapper(OrderMapper.class);
        mapper.insert(order);
        MyBatisUtils.commitAndClose();

        // 把 order 中的 订单项集合 添加到订单项表中
        OrderItemService service = new OrderItemService();
        service.addItems(order.getItemList());

    }


    @Test
    public void test01(){

        // 构建一个订单数据
        Order order = new Order();
        order.setOid("O0123");
        order.setOrdertime(new Date());
        order.setTotalPrice(100.0);
        order.setState(1);
        order.setAddress("北京");
        order.setName("张三");
        order.setTelephone("1111111");
        order.setUid("f55b7d3a352a4f0782c910b2c70f1ea4");

        OrderItem orderItem1 = new OrderItem("OI001", 10, 5.0, "1", "O0123");
        OrderItem orderItem2 = new OrderItem("OI002", 11, 8.0, "8", "O0123");

        order.getItemList().add(orderItem1);
        order.getItemList().add(orderItem2);


        // 测试 addOrder方法
        OrderService service = new OrderService();
        service.addOrder(order);

    }

}
